package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class DishServiceImpl implements DishService {


    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;



    /**
     * 新增菜品和对应口味
     * @param dishDTO
     */
    @Transactional      // 操作两张数据，开启事物管理
    @Override
    public void addDishAndFlavor(DishDTO dishDTO) {

        Dish dish = new Dish();
        // 拷贝属性
        BeanUtils.copyProperties(dishDTO,dish);

        // 向菜品表插入一条数据，并进行主键返回
        dishMapper.insert(dish);

        // 获取生成的菜品的ID
        Long dishId = dish.getId();

        // 向口味表n条数据,获取口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        // 判断口味中是否有数据
        if(flavors.size() != 0 || flavors != null){
            // 遍历数据设置菜品id
            flavors.forEach(dishFlavor ->{
                    dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }


    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult selectDishPage(DishPageQueryDTO dishPageQueryDTO) {

        // 设置默认页码色显示的条数
        if(dishPageQueryDTO.getPage() == 0){
            dishPageQueryDTO.setPage(1);
        }
        if(dishPageQueryDTO.getPageSize() == 0){
            dishPageQueryDTO.setPageSize(10);
        }
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.selectDishPage(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * 菜品的批量删除
     * @param ids
     */
    @Transactional
    @Override
    public void deleteDish(List<Long> ids) {
        // 判断当前菜品状态是否可以删除
        for (Long id : ids) {
            // 通过id查询数据
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        // 判断菜品是否被套餐关联
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishId(ids);
        if(setmealIds != null && setmealIds.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        // 删除菜品数据
        dishMapper.deleteDishByIds(ids);
        // 删除菜品对应的口味数据
        dishFlavorMapper.deleteDishFlavorByIds(ids);
    }


    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO selectDishById(Long id) {
        // 查询菜品信息
        Dish dish = dishMapper.getById(id);

        // 根据菜品ID查询口味信息
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectFlavorByDishId(id);

        // 将查询到的数据进行封装
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }


    /**
     * 修改菜品信息和对应的口味信息
     * @param dishDTO
     */

    @Override
    @Transactional
    public void updateDishAndFlavor(DishDTO dishDTO) {
        // 修改菜品基本信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);

        // 删除原有的口味信息，添加新的口味信息（修改效果）
        dishFlavorMapper.deleteDishFlavorByDishId(dishDTO.getId());
        // 向口味表n条数据,获取口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        // 判断口味中是否有数据
        if(flavors.size() != 0 || flavors != null) {
            // 遍历数据设置菜品id
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }


    /**
     * 查询菜品信息和口味信息
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> selectDishAndFlavor(Dish dish) {
        // 查询所以启售中的菜品信息
        List<Dish> dishList = dishMapper.selectAll(dish);
        List<DishVO> dishVOList = new ArrayList<>();
        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectFlavorByDishId(d.getId());
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }


    /**
     * 菜品启售和停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.updateDish(dish);
    }
}
